package com.liner.list;

import java.util.ArrayList;
import java.util.List;

/**
 * 获取子集 sublist
 */
public class SubListDemo {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            list.add(i * 10);
        }
        System.out.println("list: " + list);

        List<Integer> sublist = list.subList(3, 8);
        System.out.println("sublist:" + sublist);

        //将子集每个元素扩大10倍
        for (int i = 0; i < sublist.size(); i++) {
            sublist.set(i, sublist.get(i) * 10);
        }
        System.out.println("sublist:" + sublist);
        //对子集的操作就是对原集对应元素的操作
        System.out.println("list:" + list);

        //对原集合修改下标是3的元素，改为1000
        list.set(3,1000);
        System.out.println("list:" + list);
        System.out.println("sublist:" + sublist);

        //对原集增删后，子集不能再进行操作了，对子集增删后，原集跟着改
        sublist.remove(2);
        System.out.println("list:" + list);
        System.out.println("sublist:" + sublist);

        list.add(2,100);
        System.out.println("list:" + list);
        System.out.println("sublist:" + sublist);   //运行时发生不支持修改异常 ConcurrentModificationException

    }

}
